"""
@author: 江同学呀
@file: flattened_field.py
@date: 2024/7/27 17:57
@desc:
    https://www.elastic.co/guide/en/elasticsearch/reference/7.17/flattened.html

    An entire JSON object as a single field value.
    作为单个字段值的整个 JSON 对象。
"""
from abc import ABC
from typing import Dict, Union, Any, Type

from espc.common.field_common import IndexOptions, Similarity
from espc.orm.model.base.base import _Base
from espc.orm.model.mapping.field.base_field.base_field import _BaseField
from espc.orm.model.mapping.field.field_attribute import BoostAttribute, IndexOptionsAttribute, DepthLimitAttribute, \
    DocValuesAttribute, EagerGlobalOrdinalsAttribute, SimilarityAttribute, NullValueAttribute, IndexAttribute, \
    IgnoreAboveAttribute, SplitQueriesOnWhitespaceAttribute
from espc.utils.build_add_attr import build_add_attr


class FlattenedField(
    _BaseField, BoostAttribute, DepthLimitAttribute, DocValuesAttribute, EagerGlobalOrdinalsAttribute,
    IgnoreAboveAttribute, IndexAttribute, IndexOptionsAttribute, NullValueAttribute, SimilarityAttribute,
    SplitQueriesOnWhitespaceAttribute, ABC
):
    """
    扁平化 字段
    """
    _field_type: str = "flattened"

    def __init__(
            self, field_name: str = None, value: Dict = None, default: Dict = None, boost: float = None,
            depth_limit: int = None, doc_values: bool = None, eager_global_ordinals: bool = None,
            ignore_above: int = None, index: bool = None, index_options: Union[str, IndexOptions] = None,
            null_value: Any = None, similarity: Union[str, Similarity] = None,
            split_queries_on_whitespace: bool = None,
    ):
        super().__init__(
            field_name=field_name, value=value, default=default, doc_values=doc_values, boost=boost,
            depth_limit=depth_limit, index=index, eager_global_ordinals=eager_global_ordinals,
            ignore_above=ignore_above, index_options=index_options, null_value=null_value, similarity=similarity,
            split_queries_on_whitespace=split_queries_on_whitespace
        )
        return

    def __get__(self, instance: _Base, owner: Type["FlattenedField"]) -> Union["FlattenedField", Dict]:
        return super(FlattenedField, self).__get__(instance=instance, owner=owner)

    def __set__(self, instance, value: Dict):
        super(FlattenedField, self).__set__(instance=instance, value=value)
        return

    @property
    def value(self) -> Dict:
        return self._value

    @value.setter
    def value(self, value: Dict):
        self._value: Dict = value
        return

    @build_add_attr
    def _build(self) -> Dict:
        mapping: Dict = {
            "type": self._field_type
        }
        return mapping

